import { login, register, resetpwd, exists, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import moment from 'moment'
const getDefaultState = () => {
  return {
    token: getToken(),
    id: '',
    name: '',
    avatar: '',
    sex: '',
    hire_date: '',
    enter_id: '',
    enter_name: '',
    enter_type: '',
    type: '',
    roles: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_SEX: (state, sex) => {
    state.sex = sex
  },
  SET_HIREDATE: (state, hire_date) => {
    state.hire_date = hire_date
  },
  SET_ENTERID: (state, enter_id) => {
    state.enter_id = enter_id
  },
  SET_ENTERNAME: (state, enter_name) => {
    state.enter_name = enter_name
  },
  SET_ENTERTYPE: (state, enter_type) => {
    state.enter_type = enter_type
  },
  SET_TYPE: (state, type) => {
    state.type = type
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username, password: password }).then(response => {
        const { obj } = response
        commit('SET_TOKEN', obj)
        setToken(obj)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  register({ commit }, userInfo) {
    const { username, password, email, telephone, realname, sex, invitecode } = userInfo
    return new Promise((resolve, reject) => {
      const date = moment().format('YYYY-MM-DDTHH:mm:ss')
      const user = { username: username.trim(),
        sex: sex,
        password: password,
        email: email,
        telephone: telephone,
        name: realname,
        hire_date: date,
        invite_code: invitecode
      }
      register(user).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  resetpwd({ commit }, userInfo) {
    const { info, password } = userInfo
    return new Promise((resolve, reject) => {
      resetpwd({ resetinfo: info, password: password }).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  exists({ commit }, userInfo) {
    const { info } = userInfo
    return new Promise((resolve, reject) => {
      exists({ info: info.trim() }).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const data = response.obj
        if (!data) {
          return reject('拉取用户信息失败，请重试')
        }

        const { id, name, type, sex, hire_date, enter_id, enterprise } = data

        commit('SET_ID', id)
        commit('SET_NAME', name)
        commit('SET_TYPE', type)
        commit('SET_AVATAR', 'https://i.loli.net/2021/06/28/DcJGorjYItBa37P.jpg')
        commit('SET_SEX', sex)
        commit('SET_HIREDATE', moment(hire_date).format('YYYY-MM-DD HH:mm:ss'))
        commit('SET_ENTERID', enter_id)
        commit('SET_ENTERNAME', enterprise.name)
        commit('SET_ENTERTYPE', enterprise.type)
        commit('SET_ROLES', [enterprise.type + '|' + type])
        data.roles = [enterprise.type + '|' + type]
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

